package nickthissen.iracing.adapters;


import java.util.ArrayList;

import nickthissen.iracing.activities.ThreadListActivity;
import nickthissen.iracing.common.DateUtils;
import nickthissen.iracing.common.Prefs;
import nickthissen.iracing.datamodels.*;
import nickthissen.iracing.datamodels.Thread;
import nickthissen.iracing.iracingforum2.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ThreadListAdapter extends ArrayAdapter<nickthissen.iracing.datamodels.Thread>
{
	private ThreadListActivity activity;
	private ArrayList<Thread> items;
	
	private boolean showButton, showAuthor, showLastAuthor, showDetails;
	
	public ThreadListAdapter(ThreadListActivity context, int resourceId, ArrayList<Thread> items)
	{
		super(context, resourceId, items);
		this.activity = context;
		this.items = items;

        showAuthor = Prefs.get(context).getBoolean(R.string.PREF_ShowAuthor, true);
        showLastAuthor = Prefs.get(context).getBoolean(R.string.PREF_ShowLastReply, true);
        showDetails = Prefs.get(context).getBoolean(R.string.PREF_ShowDetails, true);
        showButton = Prefs.get(context).getBoolean(R.string.PREF_ThreadListShowLatestReplyButton, true);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View v = convertView;
		if (v == null)
		{
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.threadrow, null);
		}
		
		Thread thread = items.get(position);
		if (thread != null)
		{
			v.setClickable(true);
			v.setOnClickListener(new ItemClickListener(thread, false));
			v.setOnLongClickListener(new ItemLongClickListener(thread));
			
			v.setOnCreateContextMenuListener(null);

            // Get UI components
			TextView lblTop = (TextView) v.findViewById(R.id.lblTop);
            TextView lblAuthor = (TextView) v.findViewById(R.id.lblAuthor);
            TextView lblViews = (TextView) v.findViewById(R.id.lblViews);
            TextView lblReplies = (TextView) v.findViewById(R.id.lblReplies);
            TextView lblPages = (TextView) v.findViewById(R.id.lblPages);
            TextView lblLastAuthor = (TextView) v.findViewById(R.id.lblLastAuthor);
            TextView lblLastTime = (TextView) v.findViewById(R.id.lblLastTime);
            LinearLayout detailsLayout = (LinearLayout) v.findViewById(R.id.detailsLayout);
            LinearLayout lastReplyLayout = (LinearLayout) v.findViewById(R.id.lastReplyLayout);
			ImageView imgIcon = (ImageView) v.findViewById(R.id.imgIcon);
			ImageView imgPoll = (ImageView) v.findViewById(R.id.imgPoll);
			ImageView imgLatestReply = (ImageView) v.findViewById(R.id.imgLatestReply);

            // Set values
			lblTop.setMaxLines(1);
			lblTop.setText(thread.Title);

            lblLastAuthor.setText(thread.LastMessageUser);
            lblLastTime.setText(DateUtils.UnixMsToShortString(thread.LastMessageTime));

            lblAuthor.setText(thread.Author);
            lblViews.setText(Integer.toString(thread.Views));
            lblPages.setText(Integer.toString(thread.PageCount));
            lblReplies.setText(Integer.toString(thread.Replies));

            // Show / hide according to options
            if (showAuthor)
                lblAuthor.setVisibility(View.VISIBLE);
            else
                lblAuthor.setVisibility(View.GONE);

            if (showDetails)
                detailsLayout.setVisibility(View.VISIBLE);
            else
                detailsLayout.setVisibility(View.GONE);

            if (showLastAuthor)
                lastReplyLayout.setVisibility(View.VISIBLE);
            else
                lastReplyLayout.setVisibility(View.GONE);

			lblTop.setTypeface(null, Typeface.NORMAL);
			
			imgIcon.setImageResource(getImageId(thread.IconName));
			if (thread.IsPoll)
            {
				imgPoll.setVisibility(View.VISIBLE);
                imgIcon.setVisibility(View.GONE);
            }
			else
            {
                imgPoll.setVisibility(View.GONE);
                imgIcon.setVisibility(View.VISIBLE);
            }

			imgLatestReply.setOnClickListener(new ItemClickListener(thread, true));
			if (showButton)
				imgLatestReply.setVisibility(View.VISIBLE);
			else
				imgLatestReply.setVisibility(View.GONE);
		}
		return v;
	}
	
	private Resources resources;
	private String packageName;
	
	private int getImageId(String name)
	{
		if (resources == null) resources = activity.getResources();
		if (packageName == null) packageName = activity.getPackageName();
		
		return resources.getIdentifier(name , "drawable", packageName);
	}
	
	private class ItemClickListener implements View.OnClickListener
	{
		private Thread thread;
		private boolean latestReply;
		
		public ItemClickListener(Thread f, boolean latestReply)
		{
			this.thread = f;
			this.latestReply = latestReply;
		}
		
		@Override
		public void onClick(View v)
		{
			activity.onItemClicked(thread, latestReply);
		}		
	}
	
	private class ItemLongClickListener implements View.OnLongClickListener
	{
		private Thread thread;
		
		public ItemLongClickListener(Thread f)
		{
			this.thread = f;
		}

		@Override
		public boolean onLongClick(View v) {
			return activity.addFavorite(thread);
		}
		
		
	}
	
}
